//
//  HBBasePopoverController.m
//  HeartBeat
//
//  Created by karong on 2023/8/23.
//

#import "HBBasePopoverController.h"

@interface HBBasePopoverController ()
@property (nonatomic, strong) UITapGestureRecognizer *bgTapGesture;
@end

@implementation HBBasePopoverController

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.navigationController.navigationBarHidden = YES;
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [self styleDidChange];
}

///暗黑模式发生变化時，在这里重新設置UI樣式
- (void)styleDidChange {
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.enableDismissWhenTapBackgroundView = NO;
    self.view.backgroundColor = UIColor.mask_black_50p;
    [self styleDidChange];
}

- (void)tapBgView {
    if (self.enableDismissWhenTapBackgroundView) {
        [self dismiss];
    }
}

- (void)dismiss {
    __weak typeof(self) weakSelf = self;
    [self dismissViewControllerAnimated:YES completion:^{
        [weakSelf dismissCompletion];
    }];
}

- (void)dismissCompletion {
    
}

- (void)setEnableDismissWhenTapBackgroundView:(BOOL)enableDismissWhenTapBackgroundView {
    _enableDismissWhenTapBackgroundView = enableDismissWhenTapBackgroundView;
    if (enableDismissWhenTapBackgroundView) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView)];
        [self.view addGestureRecognizer:tap];
        self.bgTapGesture = tap;
    } else {
        [self.view removeGestureRecognizer:self.bgTapGesture];
        self.bgTapGesture = nil;
    }
}
@end
